using System;
using System.Drawing;
using System.Runtime.InteropServices;

using Pegasus.Windows.API;
using Pegasus.Windows.API.Enumerations;
using Pegasus.Windows.API.Interfaces;
using Pegasus.Windows.API.Structures;

namespace Pegasus.Windows.Shell
{
	/// <summary>
	/// This class is the base class for all windows shell items.
	/// </summary>
	public abstract class ShellItem : IDisposable
	{
		// Local Instance Values
		private Pidl m_pidl = null;
		private string m_name = null;

		private bool m_isHidden = false;
		
		private Icon m_icon = null;
		private int m_imageIndex = -1;

		/// <summary>
		/// Initializes a new instance of the <see cref="ShellItem"/> class.
		/// </summary>
		public ShellItem( Pidl pidl )
		{
			InitizlizeShellItem( pidl );
		}

		/// <summary>
		/// Gets the point to the item identifier list (PIDL).
		/// </summary>
		/// <value>The PIDL.</value>
		protected Pidl Pidl
		{
			get
			{
				return m_pidl;
			}
		}

		/// <summary>
		/// Gets the name of the item.
		/// </summary>
		/// <value>The name.</value>
		public virtual string Name
		{
			get
			{
				return m_name;
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is hidden.
		/// </summary>
		/// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
		public bool IsHidden
		{
			get
			{
				return m_isHidden;
			}
		}

		/// <summary>
		/// Gets the icon for this object.
		/// </summary>
		/// <value>The icon.</value>
		public virtual Icon Icon
		{
			get
			{
				return m_icon;
			}
		}

		/// <summary>
		/// Gets the index of the image in the System Image Lists.
		/// </summary>
		/// <value>The index of the image.</value>
		public virtual int ImageIndex
		{
			get
			{
				return m_imageIndex;
			}
		}

		/// <summary>
		/// Gets the root shell folder.
		/// </summary>
		/// <value>The root shell folder.</value>
		protected virtual IShellFolder RootShellFolder
		{
			get
			{
				return WindowsShell.RootShellFolder;
			}
		}

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		public virtual void Dispose()
		{
			if( m_pidl != null )
			{
				m_pidl.Dispose();
				m_pidl = null;
			}
		}

		/// <summary>
		/// Initizlizes the shell item.
		/// </summary>
		/// <param name="pidl">The pidl for this item.</param>
		protected virtual void InitizlizeShellItem( Pidl pidl )
		{
			m_pidl = pidl;

			// Get the information about this object.
			SHFILEINFO fileInfo = new SHFILEINFO();
			Shell32.SHGetFileInfo( m_pidl, FILE_ATTRIBUTE.NONE, ref fileInfo, Marshal.SizeOf( typeof( SHFILEINFO ) ), 
				SHGFI.PIDL | SHGFI.ATTRIBUTES | SHGFI.DISPLAYNAME | SHGFI.ICON );

			// Initialize all the instance values
			m_name = fileInfo.DisplayName;

			m_isHidden = ( ( fileInfo.Attributes & SFGAO.HIDDEN ) > 0 );
			
			
			m_icon = Icon.FromHandle( fileInfo.hIcon );
			m_imageIndex = fileInfo.IconIndex;

			// Make sure that the system lists are updated correctly.
			WindowsShell.NewShellItem( this );
		}
	}
}
